import axios, {AxiosInstance} from "axios";

const axiosUrl: AxiosInstance = axios.create({
    baseURL:'http://localhost:8080/api' ,
});

//保存cookie 
axiosUrl.defaults.withCredentials=true;//配置为true

// Add a request interceptor
axiosUrl.interceptors.request.use(function (config) {
    console.log('发请求啦', config)
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
axiosUrl.interceptors.response.use(function (response) {
    console.log("收到响应",response);
    //未登录时跳转到登录页
    if(response?.data?.code === 40100) {
        const redirectUrl = window.location.href;
        window.location.href = `/user/login?redirect=${redirectUrl}`;
    }
    return response?.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default axiosUrl;
